package com.shangyitong.yygh_hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangyitong.ResultCodeEnum;
import com.shangyitong.exception.HospitalException;
import com.shangyitong.yygh.model.hosp.HospitalSet;
import com.shangyitong.yygh.vo.hosp.HospitalSetQueryVo;
import com.shangyitong.yygh.vo.order.SignInfoVo;
import com.shangyitong.yygh_hosp.mapper.HospitalSetMapper;
import com.shangyitong.yygh_hosp.service.HospitalSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public Page<HospitalSet> findPageHospSet(Long current, Long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Long start = (current - 1) * limit;
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        Long total = hospitalSetMapper.getCount(hoscode, hosname);
        List<HospitalSet> hospitalSetList = hospitalSetMapper.getPage(start, limit, hoscode, hosname);
        Page<HospitalSet> page = new Page<>();
        page.setRecords(hospitalSetList);
        page.setTotal(total);
        page.setCurrent(start);
        page.setSize(total);
        return page;
    }
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        HospitalSet hospitalSet = baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        if (null == hospitalSet){
            throw new HospitalException(ResultCodeEnum.DATA_ISNULL);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }
}
